package com.book.controller;

import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.lang.Snowflake;
import com.book.util.ResultBack;
import com.book.vo.CodeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @program:
 * @ClassName:
 * @description:
 * @author: zgc
 * @date:
 * @Version 1.0
 **/
@Controller
@Slf4j
public class CodeController {
    @Resource
    LineCaptcha lineCaptcha;
    @Resource
    Snowflake snowflake;
    @Resource
    Map<String,String> map;

    @GetMapping("/getCode")
    @ResponseBody
    public ResultBack getCode(HttpServletResponse response) throws IOException {
        lineCaptcha.createCode();
        //输出code
        String code = lineCaptcha.getCode();

        // 再生成一个ID，让后端，可以识别到前端：
        long codeId = snowflake.nextId();

        // 把生成的codeId 和 code 进行缓存
        map.put(codeId+"",code);

        // 这里改成了，生成base64编码图片，方便我们处理。
        String imageBase64Data = lineCaptcha.getImageBase64Data();
        //创建codeVo对象
        CodeVo codeVo = new CodeVo();
        
        codeVo.setCodeId(codeId+"");
        codeVo.setCode(imageBase64Data);

        return ResultBack.success("生成验证码成功", codeVo);
    }
}
